package com.liuchao.maven.practice.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description:
 * @Auther: Liu Chao
 * @Date: 2018/8/11 07:30
 */
public class Send {
    public static final String QUEUE_NAME = "test_queue_confirm";

    public static void main(String[] args) throws IOException, TimeoutException, InterruptedException {
        Connection connection = RabbitMQUtil.getConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//        try {
//            channel.txSelect();
        channel.confirmSelect();
            channel.basicQos(1);
            for (int i=0;i<50;i++) {
                String msg = "hello confirm! " + i;
                channel.basicPublish("", QUEUE_NAME , null, msg.getBytes());
                System.out.println("----> send msg");
            }
//            int i = 1/0;
//            channel.txCommit();
//        } catch (Exception e) {
//            channel.txRollback();
//            System.out.println("msg tx rollback");
//        }
        if (channel.waitForConfirms()) {
            System.out.println("----> send msg success");
        } else {
            System.out.println("----> send msg fail");
        }

        channel.close();
        connection.close();

    }
}
